package com.fancy.learncenter.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTeacherListClassDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonTeacherClassListAdapter;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonSeachDetailActivity extends BaseActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.search_et)
    EditText searchEt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_seach_detail, Status.STATUS);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        cartoonClassListAdapter = new CartoonTeacherClassListAdapter(this, new ArrayList<CartoonTeacherListClassDataBean.ClassVOBean>());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(cartoonClassListAdapter);

        if (!TextUtils.isEmpty(getIntent().getStringExtra("className"))) {
            searchEt.setText(getIntent().getStringExtra("className"));
            getTeacherClassList();
        }

    }

    CartoonTeacherClassListAdapter cartoonClassListAdapter;

    @OnClick({R.id.back, R.id.search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.search:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }

                getTeacherClassList();

                break;
        }
    }

    private void getTeacherClassList() {
        if (TextUtils.isEmpty(searchEt.getText().toString())) {
            ToastUtil.show("请输入班级名称");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("className", searchEt.getText().toString());
        HttpMehtod.getInstance().getListClass(map, new CustomNetSubscriber(this, false) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {


                BaseBean<CartoonTeacherListClassDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherListClassDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherListClassDataBean>() {
                    @Override
                    public void success(CartoonTeacherListClassDataBean dataBean) {


                        if (dataBean == null || dataBean.getClassVO() == null) {
                            cartoonClassListAdapter.notifyDataSetChanged(new ArrayList<CartoonTeacherListClassDataBean.ClassVOBean>());

                        } else {
                            cartoonClassListAdapter.notifyDataSetChanged(dataBean.getClassVO());

                        }


                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }
}
